﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Xc.DataAccess.Core;
using Xc.DataAccess.PcpChannels;

namespace Xc.DataAccess.Protocoals.PcpChannels.Services
{
    /// <summary>
    /// PCP升级服务
    /// </summary>
    public class PcpUpgradeService<TChannel> where TChannel:IChannel
    {
        /// <summary>
        /// 设备通道
        /// </summary>
        public  TChannel Channel { get; private set; }
        public PcpChannelOption Option { get; set; }=new PcpChannelOption();
        public PcpUpgradeService(TChannel channel1)
        {
            this.Channel = channel1;
        }

        /// <summary>
        /// 发起请求
        /// </summary>
        /// <param name="request"></param>
        /// <param name="validateCallBack"></param>
        /// <returns></returns>
        private async Task<ResultData<byte[]>> RequestCore(byte[] request, Func<byte[], bool> validateCallBack) 
        {
           return await  Channel.Request(request, Option.Timeout, validateCallBack);
        }
        /// <summary>
        /// 发起请求
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task<ResultData<TRespond>> RequestAsync<TRespond>(IRequestFullFrame request) where TRespond: RespondFrame
        {
            var res=  await this.RequestCore(request.FullFrame, (datas) =>
            {
                if (datas==null||datas.Length==0)
                {
                    return false;
                }
                RespondFrame respondFrame = new RespondFrame(datas);
                return respondFrame.IsValidate;
            });
            if (res.IsSuccess)
            {
                return ResultData<TRespond>.Accept(res.Message,(TRespond)Activator.CreateInstance(typeof(TRespond),new object[] { res .Data}));
            }
            else
            {
                return ResultData<TRespond>.Reject(res.Message,default);
            }
        }
        /// <summary>
        /// 执行升级
        /// </summary>
        /// <returns></returns>
        public async Task<ResultData<CloudExecuteUpdateRespond>> ExecuteUpdate()
        {
           var res=await  this.RequestAsync<CloudExecuteUpdateRespond>(new CloudExecuteUpdateRequest());
            return res;
        }
        /// <summary>
        /// 新版本通知请求
        /// </summary>
        /// <param name="desertVersion"></param>
        /// <param name="packageSize"></param>
        /// <param name="packageCount"></param>
        /// <param name="packageCheck"></param>
        /// <returns></returns>
        public async Task<ResultData<CloundNewVersionNotifyRespond>> NewVersionNotify(string desertVersion,ushort packageSize,ushort packageCount,ushort packageCheck)
        {
            return await this.RequestAsync<CloundNewVersionNotifyRespond>(new CloundNewVersionNotifyRequest {DesertVersion=desertVersion,PackageSize=packageSize,PackageCheck=packageCheck,PackageCount=packageCount });
        }
        /// <summary>
        /// 查询设备版本
        /// </summary>
        /// <returns></returns>
        public async Task<ResultData<CloundQueryDeviceVersionRespond>> QueryDeviceVersion()
        {
            return await this.RequestAsync<CloundQueryDeviceVersionRespond>(new CloundQueryDeviceVersionReuqest());
        }

    }
}
